added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBASPNETReverseAJAX / Client.vb
blobd1d8bbf575c6a02a949145e4e0cdc39b17072b3f
1 '****************************** Module Header ******************************\
2 ' Module Name: Client.vb
3 ' Project: VBASPNETReverseAJAX
4 ' Copyright (c) Microsoft Corporation
6 ' Client class is used to synchronize the message sending and the message receiving.
7 ' When DequeueMessage method is called, the method will wait until a new message
8 ' is inserted by calling EnqueueMessage method. This class benefits ManualResetEvent
9 ' class to achieve the synchronism.
11 ' This source is subject to the Microsoft Public License.
12 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 ' All other rights reserved.
15 '*****************************************************************************/
17 Imports System.Collections.Generic
18 Imports System.Threading
20 ''' <summary>
21 ''' This class represents a web client which can receive messages.
22 ''' </summary>
23 Public Class Client
24 Private messageEvent As New ManualResetEvent(False)
25 Private messageQueue As New Queue(Of Message)()
27 ''' <summary>
28 ''' This method is called by a sender to send a message to this client.
29 ''' </summary>
30 ''' <param name="message">the new message</param>
31 Public Sub EnqueueMessage(ByVal message As Message)
32 SyncLock messageQueue
33 messageQueue.Enqueue(message)
35 ' Set a new message event.
36 messageEvent.[Set]()
37 End SyncLock
38 End Sub
40 ''' <summary>
41 ''' This method is called by the client to receive messages from the message queue.
42 ''' If no message, it will wait until a new message is inserted.
43 ''' </summary>
44 ''' <returns>the unread message</returns>
45 Public Function DequeueMessage() As Message
46 ' Wait until a new message.
47 messageEvent.WaitOne()
49 SyncLock messageQueue
50 If messageQueue.Count = 1 Then
51 messageEvent.Reset()
52 End If
53 Return messageQueue.Dequeue()
54 End SyncLock
55 End Function
56 End Class